package com.atguigu.gmall.product.service.impl;

import com.atguigu.gamll.feign.search.SearchFeignClient;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.entity.*;
import com.atguigu.gmall.product.mapper.SkuInfoMapper;
import com.atguigu.gmall.product.service.*;
import com.atguigu.gmall.product.vo.CategoryViewDTO;
import com.atguigu.gmall.product.vo.SaveSkuVo;
import com.atguigu.gmall.search.Goods;
import com.atguigu.gmall.search.SearchAttr;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 10574
 * @description 针对表【sku_info(库存单元表)】的数据库操作Service实现
 * @createDate 2022-11-29 17:33:22
 */
@Slf4j
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
        implements SkuInfoService {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Resource
    SkuImageService skuImageService;

    @Resource
    SkuAttrValueService skuAttrValueService;

    @Resource
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Resource
    SearchFeignClient searchFeignClient;

    @Resource
    SkuInfoService skuInfoService;

    @Resource
    BaseTrademarkService baseTrademarkService;

    @Resource
    BaseCategory1Service baseCategory1Service;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveSkuInfo(SaveSkuVo vo) {
        //保存库存单元表sku_info
        SkuInfo skuInfo = new SkuInfo();
        BeanUtils.copyProperties(vo, skuInfo);
        this.save(skuInfo);
        Long skuId = skuInfo.getId();

        //保存库存单元图片表sku_image
        List<SkuImage> skuImages = vo.getSkuImageList()
                .stream()
                .map(item -> {
                    SkuImage skuImage = new SkuImage();
                    BeanUtils.copyProperties(item, skuImage);
                    skuImage.setSkuId(skuId);
                    return skuImage;
                }).collect(Collectors.toList());
        skuImageService.saveBatch(skuImages);

        //保存平台属性值关联表sku_attr_value
        List<SkuAttrValue> skuAttrValues = vo.getSkuAttrValueList()
                .stream()
                .map(item -> {
                    SkuAttrValue skuAttrValue = new SkuAttrValue();
                    BeanUtils.copyProperties(item, skuAttrValue);
                    skuAttrValue.setSkuId(skuId);
                    return skuAttrValue;
                }).collect(Collectors.toList());
        skuAttrValueService.saveBatch(skuAttrValues);

        //保存sku销售属性值sku_sale_attr_value
        List<SkuSaleAttrValue> skuSaleAttrValues = vo.getSkuSaleAttrValueList()
                .stream()
                .map(item -> {
                    SkuSaleAttrValue skuSaleAttrValue = new SkuSaleAttrValue();
                    skuSaleAttrValue.setSkuId(skuId);
                    skuSaleAttrValue.setSpuId(skuInfo.getSpuId());
                    skuSaleAttrValue.setSaleAttrValueId(item.getSaleAttrValueId());
                    return skuSaleAttrValue;
                }).collect(Collectors.toList());
        skuSaleAttrValueService.saveBatch(skuSaleAttrValues);

        redisTemplate.opsForValue().setBit(RedisConst.SKUID_BITMAP,skuId,true);
    }

    @Override
    public void onSale(Long skuId) {
        boolean update = lambdaUpdate().set(SkuInfo::getIsSale, 1).eq(SkuInfo::getId, skuId).update();
        if (update){
            //远程调用search服务，将商品添加到es中
            Goods goods = getGoods(skuId);
            //保存es
            searchFeignClient.up(goods);
        }
    }

    @Override
    public void cancelSale(Long skuId) {
        boolean update = lambdaUpdate().set(SkuInfo::getIsSale, 0).eq(SkuInfo::getId, skuId).update();
        if (update){
            searchFeignClient.down(skuId);
            log.info("商品下架成功{}",skuId);
        }
    }

    @Override
    public SkuInfo getSkuInfo(Long skuId) {
        SkuInfo skuInfo = getById(skuId);
        return skuInfo;
    }

    private Goods getGoods(Long skuId) {
        Goods goods = new Goods();
        SkuInfo skuInfo = skuInfoService.getById(skuId);

        goods.setId(skuInfo.getId());
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        goods.setPrice(skuInfo.getPrice().doubleValue());
        goods.setCreateTime(new Date());
        //查询品牌
        BaseTrademark trademark = baseTrademarkService.getById(skuInfo.getTmId());
        goods.setTmId(trademark.getId());
        goods.setTmName(trademark.getTmName());
        goods.setTmLogoUrl(trademark.getLogoUrl());
        //三级分类属性
        CategoryViewDTO categoryViewDTO = baseCategory1Service.getCategoryTreeWithC3Id(skuInfo.getCategory3Id());
        goods.setCategory1Id(categoryViewDTO.getCategory1Id());
        goods.setCategory1Name(categoryViewDTO.getCategory1Name());
        goods.setCategory2Id(categoryViewDTO.getCategory2Id());
        goods.setCategory2Name(categoryViewDTO.getCategory2Name());
        goods.setCategory3Id(categoryViewDTO.getCategory3Id());
        goods.setCategory3Name(categoryViewDTO.getCategory3Name());

        //热度
        goods.setHotScore(0L);
        //所有的平台属性
        List<SearchAttr> searchAttrs=skuAttrValueService.getSkuAttrValue(skuId);
        goods.setAttrs(searchAttrs);
        return goods;
    }
}




